﻿using System.Linq;
using Beaker;
using Beaker.Specs;
using OpenCube.Nbt.Mapping;
using OpenCube.Nbt.Specs.Builders;

namespace OpenCube.Nbt.Specs.Mapping.Converters
{
	public abstract class SpecsForSimpleConverter<TTypeConverter, TModel, TNbt>
		: SpecsFor<INbtTypeConverter, TypeConverterBuilder<TTypeConverter>>
		where TTypeConverter : NbtSimpleTypeConverter<TModel, TNbt>, INbtTypeConverter
	{
		protected TModel ConvertToModelValue(TNbt nbtValue)
		{
			var dict = new NbtDictionary();
			dict.Add("value", nbtValue);
			var queryContainer = new QueryContainerStub();
			var conversionContext = new ConversionContext(queryContainer, nbtDictionary: dict);
			return (TModel)Subject.ConvertToModelValue(conversionContext, new object[] { nbtValue });
		}

		protected Maybe<TNbt> ConvertToNbtValue(TModel modelValue)
		{
			var queryContainer = new QueryContainerStub();
			var conversionContext = new ConversionContext(queryContainer, nbtDictionary: new NbtDictionary());
			var list = Subject.ConvertToNbtValue(conversionContext, modelValue).ToList();
			if( list.Count == 0 )
				return new Maybe<TNbt>();
			return new Maybe<TNbt>((TNbt)list[0]);
		}
	}
}
